#pragma once

#include <deque>
#include <memory>
#include "NetUtil.h"
#include "ConnectorImpl.h"

namespace wsbus {

	class Connector;
	typedef std::shared_ptr<beast::flat_buffer> BufferPtr;
	typedef std::shared_ptr<Connector> ConnectorPtr;

	class Connector
	{
	public:
		Connector(const std::string &host, const std::string &port);
		~Connector();
		void connect();
		void reconnect();

		void setHandleCallback(const ConnectorImpl::HandleCallback &cb);
		void post(const BufferPtr &msg);
		time_t lastWriteMessageTime() const;
		void runMainContext(const std::function<void()> &cb);

	private:
		net::io_context ioc_;
#ifdef WS_SSL
		ssl::context sslctx_{ ssl::context::tlsv12_client };
#endif
		std::thread thread_;
		ConnectorImplPtr impl_;
	};

}